<template>
  <div class="customer-list">
    <div class="page-header">
      <h2>客户列表</h2>
      <el-button type="primary" @click="$router.push('/customers/add')">添加客户</el-button>
    </div>
    
    <div class="search-bar">
      <el-input
        v-model="searchName"
        placeholder="请输入客户姓名或邮箱"
        class="search-input"
        clearable>
      </el-input>
      <el-button type="primary" @click="searchCustomers">搜索</el-button>
      <el-button @click="resetSearch">重置</el-button>
    </div>
    
    <el-table
      v-loading="loading"
      :data="customers"
      border
      style="width: 100%">
      <el-table-column
        prop="customerId"
        label="客户ID"
        width="80">
      </el-table-column>
      <el-table-column
        prop="name"
        label="客户姓名"
        min-width="120">
      </el-table-column>
      <el-table-column
        prop="email"
        label="电子邮箱"
        min-width="180">
      </el-table-column>
      <el-table-column
        prop="phone"
        label="联系电话"
        min-width="120">
      </el-table-column>
      <el-table-column
        prop="address"
        label="联系地址"
        min-width="180">
      </el-table-column>
      <el-table-column
        label="操作"
        width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      class="pagination">
    </el-pagination>
  </div>
</template>

<script>
import customerApi from '@/api/customer'

export default {
  name: 'CustomerList',
  
  data() {
    return {
      loading: true,
      customers: [],
      allCustomers: [],
      searchName: '',
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  
  created() {
    this.fetchCustomers()
  },
  
  methods: {
    // 获取客户列表
    async fetchCustomers() {
      this.loading = true
      try {
        const response = await customerApi.getAllCustomers()
        this.allCustomers = response.data
        this.filterCustomers()
        this.loading = false
      } catch (error) {
        this.$message.error('获取客户列表失败')
        this.loading = false
      }
    },
    
    // 过滤客户
    filterCustomers() {
      let filtered = [...this.allCustomers]
      
      // 按名称或邮箱搜索
      if (this.searchName) {
        const search = this.searchName.toLowerCase()
        filtered = filtered.filter(item => 
          (item.name && item.name.toLowerCase().includes(search)) ||
          (item.email && item.email.toLowerCase().includes(search))
        )
      }
      
      this.total = filtered.length
      
      // 分页
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      this.customers = filtered.slice(start, end)
    },
    
    // 搜索客户
    searchCustomers() {
      this.currentPage = 1
      this.filterCustomers()
    },
    
    // 重置搜索
    resetSearch() {
      this.searchName = ''
      this.currentPage = 1
      this.filterCustomers()
    },
    
    // 编辑客户
    handleEdit(row) {
      this.$router.push(`/customers/edit/${row.customerId}`)
    },
    
    // 删除客户
    async handleDelete(row) {
      try {
        await this.$confirm('此操作将永久删除该客户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        
        const response = await customerApi.deleteCustomer(row.customerId)
        if (response.data.success) {
          this.$message.success(response.data.message)
          if (this.customers.length === 1) {
            this.currentPage--
          }
          this.fetchCustomers()
        } else {
          this.$message.error(response.data.message)
        }
      } catch (error) {
        if (error !== 'cancel') {
          this.$message.error('删除客户失败')
        }
      }
    },
    
    // 页面大小变化
    handleSizeChange(val) {
      this.pageSize = val
      this.filterCustomers()
    },
    
    // 页码变化
    handleCurrentChange(val) {
      this.currentPage = val
      this.filterCustomers()
    }
  }
}
</script>

<style scoped>
.customer-list {
  background-color: #fff;
  padding: 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.search-bar {
  margin-bottom: 20px;
  display: flex;
  gap: 10px;
}

.search-input {
  width: 200px;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}
</style> 